package com.string.actortv.util;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class MyHttpUtil {

    public static final int RETRY_TIMES = 3;

    public static void getAsync(String linkUrl, AsyncCallBack callback, Map<String, String> headerMap) {
        Log.d("request_get", linkUrl);
        //开启线程，发送请求
        Thread thread = new Thread(() -> {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(linkUrl);
                connection = (HttpURLConnection) url.openConnection();
                //设置请求方法
                connection.setRequestMethod("GET");
                //设置连接超时时间（毫秒）
                connection.setConnectTimeout(5000);
                //设置读取超时时间（毫秒）
                connection.setReadTimeout(5000);
                if (headerMap != null) {
                    for (String key : headerMap.keySet()) {
                        //设置header
                        connection.setRequestProperty(key, headerMap.get(key));
                    }
                }

                //返回输入流
                InputStream in = connection.getInputStream();

                //读取输入流
                reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                callback.execute(result.toString());
            } catch (IOException e) {
                callback.execute("");
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (connection != null) {//关闭连接
                    connection.disconnect();
                }
            }
        });
        thread.start();
    }

    public static String get(String linkUrl) {
        Log.d("request_get", linkUrl);
        final String[] res = {""};
        //开启线程，发送请求
        Thread thread = new Thread(() -> {
            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(linkUrl);
                connection = (HttpURLConnection) url.openConnection();
                //设置请求方法
                connection.setRequestMethod("GET");
                //设置连接超时时间（毫秒）
                connection.setConnectTimeout(5000);
                //设置读取超时时间（毫秒）
                connection.setReadTimeout(5000);

                //返回输入流
                InputStream in = connection.getInputStream();

                //读取输入流
                reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }
                res[0] = result.toString();
            } catch (IOException ignored) {
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (connection != null) {//关闭连接
                    connection.disconnect();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return res[0];
    }

    public interface AsyncCallBack {
        void execute(String result);
    }

}
